//
//  CZMainTabbarView.m
//  globalTranslate
//
//  Created by 陈镇 on 2025/8/9.
//

#import "CZMainTabbarView.h"

@interface CZMainTabbarView ()

@property(nonatomic,strong) NSArray *titles;
@property(nonatomic,strong) NSArray *images;
@property(nonatomic,strong) NSArray *selectImages;

@property(nonatomic,strong) QMUIButton *selectBtn;
@end

@implementation CZMainTabbarView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.whiteColor;
        self.qmui_borderPosition = QMUIViewBorderPositionTop;
        self.qmui_borderColor = UIColorHex(eeeeee);
        self.qmui_borderWidth = .5;
        
    
        [self createItems];
        
    }
    return self;
}

-(void)createItems{
    
    
    CGFloat width = SCREEN_WIDTH/self.titles.count;
    for (int i = 0; i < self.titles.count; i++) {
        
        QMUIButton *btn = [QMUIButton createButtonTitle:self.titles[i] titleColor:UIColorHex(999999) font:SLFontMakeRegular(12)];
        [btn setImage:[UIImage imageNamed:self.images[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:self.selectImages[i]] forState:UIControlStateSelected];
        [btn setTitleColor:UIColorHex(#969696) forState:UIControlStateSelected];
        [btn setTitleColor:UIColorHex(#0D6886) forState:UIControlStateSelected];
        btn.frame = CGRectMake(i*width, 0, width, 49);
        btn.imagePosition = QMUIButtonImagePositionTop;
        [self addSubview:btn];
        [btn addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i + 100;
        if (i == 0) {
            [self changeBtnStatus:btn];
        }
        
//        btn.backgroundColor = UIColor.greenColor;
    }
    
    
}

-(NSArray *)titles{
    if (!_titles) {
        _titles = @[@"文字",@"语音",@"图片",@"我的",];
    }
    return _titles;
}

-(NSArray *)images{
    if (!_images) {
        _images = @[@"text_n",@"voice_n",@"photo",@"mycenter",];
    }
    return _images;
}
-(NSArray *)selectImages{
    if (!_selectImages) {
        _selectImages = @[@"text_h",@"voice_h",@"photo_h",@"mycenter_h"];
    }
    return _selectImages;
}

-(void)changeBtnStatus:(QMUIButton *)btn{
    _selectBtn.selected = NO;
    _selectBtn = btn;
    _selectBtn.selected = YES;
}


-(void)clickItem:(QMUIButton *)btn{
    NSInteger index = btn.tag - 100;
    [self changeBtnStatus:btn];
    if (_delegate && [_delegate respondsToSelector:@selector(mainTabbarViewClickItemWithIndex:)]) {
        [_delegate mainTabbarViewClickItemWithIndex:index];
    }
    
}

-(void)selectToIndex:(NSInteger)index{
    
    QMUIButton *btn = (QMUIButton *)[self viewWithTag:index + 100];
    [self changeBtnStatus:btn];
 
}
@end
